import threading
import time

import serial


class SerialPortScreen(threading.Thread):
    def __init__(self, sername, baudrate):
        threading.Thread.__init__(self, name="GPSSerialListener")
        self.open(sername, baudrate)

    def __del__(self):
        self.__ser.close()

    def open(self, port, baudrate):
        '''串口连接'''
        timeout = None  # 超时时间设置，None代表永远等待操作
        self.__ser = serial.Serial(port, baudrate, timeout=timeout)  # 打开串口，并得到串口对象

        if self.__ser.isOpen:
            self.__ser.close()
            self.__ser.open()
        print('GPS串口详情参数：', self.__ser)

    def setSpeed(self, data):
        data = str(data)
        if len(data) == 1:
            speed = 'EE B1 10 00 00 00 0B 3' + str(data) + ' FF FC FF FF'  # 速度
        if len(data) == 2:
            datatens = data[0]
            dataones = data[1]
            speed = 'EE B1 10 00 00 00 0B 3' + str(datatens) + ' ' + '3' + str(dataones) + ' FF FC FF FF'  # 速度
        speed = bytes.fromhex(speed)
        return speed

    def setArea(self, data):
        data = str(data)
        if len(data) == 1:
            area = 'EE B1 10 00 00 00 0C 3' + str(data) + ' FF FC FF FF'  # 亩
        if len(data) == 2:
            datatens = data[0]
            dataones = data[1]
            area = 'EE B1 10 00 00 00 0C 3' + str(datatens) + ' ' + '3' + str(dataones) + ' FF FC FF FF'  # 亩
        area = bytes.fromhex(area)
        return area

    def setDistance(self, data):
        data = str(data)
        if len(data) == 1:
            distance = 'EE B1 10 00 00 00 07 3' + str(data) + ' 20 6B 6D FF FC FF FF'  # 里程
        if len(data) == 2:
            datatens = data[0]
            dataones = data[1]
            distance = 'EE B1 10 00 00 00 07 3' + str(datatens) + ' ' + '3' + str(
                dataones) + ' 20 6B 6D FF FC FF FF'  # 里程
        if len(data) == 3:
            hundreds = data[0]
            datatens = data[1]
            dataones = data[2]
            distance = 'EE B1 10 00 00 00 07 3' + str(hundreds) + ' ' + '3' + str(datatens) + ' ' + '3' + str(
                dataones) + ' 20 6B 6D FF FC FF FF'  # 里程
        distance = bytes.fromhex(distance)
        return distance

    def setAllArea(self, data):
        data = str(data)
        if len(data) == 1:
            allarea = 'EE B1 10 00 00 00 08 3' + str(data) + ' 20 C4 B6 FF FC FF FF'  # 累计面积
        if len(data) == 2:
            datatens = data[0]
            dataones = data[1]
            allarea = 'EE B1 10 00 00 00 08 3' + str(datatens) + ' ' + '3' + str(
                dataones) + ' 20 C4 B6 FF FC FF FF'  # 累计面积
        if len(data) == 3:
            hundreds = data[0]
            datatens = data[1]
            dataones = data[2]
            allarea = 'EE B1 10 00 00 00 08 3' + str(hundreds) + ' ' + '3' + str(datatens) + ' ' + '3' + str(
                dataones) + ' 20 C4 B6 FF FC FF FF'  # 累计面积
        allarea = bytes.fromhex(allarea)
        return allarea

    def setDayAddArea(self, data):
        data = str(data)
        if len(data) == 1:
            dayAddArea = 'EE B1 10 00 00 00 09 3' + str(data) + ' 20 C4 B6 FF FC FF FF'  # 本日新增
        if len(data) == 2:
            datatens = data[0]
            dataones = data[1]
            dayAddArea = 'EE B1 10 00 00 00 09 3' + str(datatens) + ' ' + '3' + str(dataones) + ' 20 C4 B6 FF FC FF FF'  # 本日新增
        if len(data) == 3:
            datahundreds = data[0]
            datatens = data[1]
            dataones = data[2]
            dayAddArea = 'EE B1 10 00 00 00 09 3' + str(datahundreds) + ' ' + '3' + str(datatens) + '3' + str(
                dataones) + ' 20 C4 B6 FF FC FF FF'  # 本日新增
        dayAddArea = bytes.fromhex(dayAddArea)
        return dayAddArea

    def setWorkTime(self, data):
        data = str(data)
        if len(data) == 2:
            stens = data[0]
            sones = data[1]
            worktime = 'EE B1 10 00 00 00 0A 30 30' + ' ' + '3A' + ' ' + '3' + str(
                stens) + ' ' + '3' + str(sones) + ' 20 6D 69 6E 73 FF FC FF FF'  # 时间
        if len(data) == 4:
            mtens = data[0]
            mones = data[1]
            stens = data[2]
            sones = data[3]
            worktime = 'EE B1 10 00 00 00 0A 3' + str(mtens) + ' ' + '3' + str(mones) + ' ' + '3A' + ' ' + '3' + str(
                stens) + ' ' + '3' + str(sones) + ' 20 6D 69 6E 73 FF FC FF FF'  # 时间
        if len(data) == 6:
            htens = data[0]
            hones = data[1]
            mtens = data[2]
            mones = data[3]
            stens = data[4]
            sones = data[5]
            worktime = 'EE B1 10 00 00 00 0A 3' + str(htens) + ' ' + '3' + str(hones) + ' ' + '3A' + ' ' + '3' + str(
                mtens) + ' ' + '3' + str(mones) + '3A' + ' ' + '3' + str(
                stens) + ' ' + '3' + str(sones) + ' 20 6D 69 6E 73 FF FC FF FF'  # 时间
        worktime = bytes.fromhex(worktime)
        return worktime

    def writeData(self, data):
        # while self.__ser.writable():
        print(data)
        try:
            self.__ser.write(data)
            # self.__ser.write(serport.setArea(data['area']))
            # self.__ser.write(serport.setDistance(data['distance']))
            # self.__ser.write(serport.setAllArea(data['allarea']))
            # self.__ser.write(serport.setDayAddArea(data['dayAddArea']))
            # self.__ser.write(serport.setWorkTime(data['worktime']))
        except Exception as ex:
            print("exception%s" % ex)


if __name__ == '__main__':
    serport = SerialPortScreen('/dev/cu.usbserial-1130', 115200)
    speed = 1
    area = 1
    distance1 = 1
    allarea = 1
    dayAddArea = 1

    i = 1
    while i < 5:
        speed += 1
        area +=1
        distance1 += 1
        allarea += 1
        dayAddArea += 1
        worktime = time.strftime("%M%S", time.localtime(time.time()))

        screen = {'speed': speed, 'area': area, 'distance': distance1, 'allarea': allarea, 'dayAddArea': dayAddArea, 'worktime': worktime}

        serport.writeData(screen)
        if speed and area >= 99:
            speed = 1
            area = 1
        if distance1 and allarea and dayAddArea >=999:
            distance1 = 1
            allarea = 1
            dayAddArea = 1